package net.soevar.action.admins.helper;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.zkoss.zul.AbstractTreeModel;

import net.soevar.database.hibernate.HibernateUtil;
import net.soevar.database.model.AthMenu;

public class MenuTreeModel extends AbstractTreeModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5115651721345571411L;

	/**
	 * Constructor
	 * 
	 * @param tree
	 *            the list is contained all data of nodes.
	 */
	public MenuTreeModel() {
		super(new AthMenu(-1, 0));
	}

	@SuppressWarnings("unchecked")
	public List<AthMenu> getChildren(AthMenu parentMenu) {
		Session session = HibernateUtil.currentSession();
		List<AthMenu> menus = session.createCriteria(AthMenu.class)
				.add(Restrictions.eq("root", parentMenu.getChild()))
				.addOrder(Order.asc("root")).addOrder(Order.asc("child"))
				.addOrder(Order.asc("label")).list();
		return menus;
	}

	public Object getChild(Object parent, int index) {
		AthMenu parentMenu = (AthMenu) parent;

		List<AthMenu> menus = getChildren(parentMenu);

		AthMenu menu = null;

		if (menus.size() < index) {
			menu = null;
		} else {
			menu = menus.get(index);
		}

		return menu;
	}

	public int getChildCount(Object parent) {
		AthMenu parentMenu = (AthMenu) parent;

		Session session = HibernateUtil.currentSession();

		Integer count = ((Number) session.createCriteria(AthMenu.class)
				.add(Restrictions.eq("root", parentMenu.getChild()))
				.setProjection(Projections.rowCount()).uniqueResult())
				.intValue();

		return count;
	}

	public boolean isLeaf(Object node) {
		return (getChildCount(node) == 0);
	}

	/**
	 * @since 5.0.6
	 * @see org.zkoss.zul.TreeModel#getIndexOfChild(java.lang.Object,
	 *      java.lang.Object)
	 */
	@Override
	public int getIndexOfChild(Object arg0, Object arg1) {
		return 0;
	}

}